package cn.signalpha.modules.com.service;

import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.com.entity.UserLevel;
import cn.signalpha.modules.com.mapper.UserLevelMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Map;

import static cn.signalpha.common.cache.CacheNames.USER_LEVEL_KEY;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class UserLevelService extends ServiceImpl<UserLevelMapper, UserLevel> {

	/**
	 * 自定义Wrapper
	 */
	public QueryWrapper<UserLevel> selectUserLevelWrapper(Map<String, Object> params) {
		QueryWrapper<UserLevel> wrapper = Wrappers.query();
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("id")), UserLevel::getId, params.get("id"));
		wrapper.lambda().like(ObjectUtil.isNotEmpty(params.get("name")), UserLevel::getName, params.get("name"));
		wrapper.lambda().orderByAsc(UserLevel::getId);
		return wrapper;
	}

	@Cacheable(cacheNames = USER_LEVEL_KEY, key = "#id", unless="#result==null")
	public UserLevel getById(Integer id) {
		return baseMapper.selectById(id);
	}
	@CacheEvict(cacheNames = USER_LEVEL_KEY, allEntries = true)
	public void removeCaChe() {
	}

	/**
	 * 等级名称查询
	 */
	public UserLevel getByName(String name) {
		return baseMapper.selectOne(Wrappers.<UserLevel>lambdaQuery().eq(UserLevel::getName, name).last("limit 1"));
	}

}

